package com.zy.design.pattern.template;

import com.zy.design.pattern.template.jdbctm.MySimpleJdbcTemplate;
import com.zy.design.pattern.template.jdbctm.UserMapper;
import com.zy.design.pattern.template.original.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhaoyi
 * @date 2021/7/28 11:15
 * 模板模式和callBack
 */
public class Ts {


    public static void main(String[] args) {

        //模拟实现一个简单的jdbcTemplate
        MySimpleJdbcTemplate tm = new MySimpleJdbcTemplate();

        //变化的sql 和对象转换 不变的流程  加载驱动,创建连接,释放,异常处理等...
        String sql = "select id, title from tb_awards limit 3;";


        tm.query2(sql, User.class).forEach(System.out::println);
        System.out.println("-----------------------------------");

        tm.query(sql, new UserMapper()).forEach(System.out::println);

        String sql2 = "select * from tb_awards limit 5;";
        tm.query(sql2, rs -> {
            List ls = new ArrayList();
            try {
                while (rs.next()) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("logo", rs.getString("logo"));
                    map.put("title", rs.getString("title"));
                    ls.add(map);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return ls;
        }).forEach(System.out::println);


    }


}
